import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    public static List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> list2 = new ArrayList<>();
        int n = nums.length;
        for (int i = 0; i < nums.length;) {
            if(nums[i] > 0) break;
            int left = i + 1;int right = n - 1;
            int target = -nums[i];
            while(left < right) {
                int sum = nums[left] + nums[right];
                if(sum > target) right--;
                else if(sum < target) left++;
                else {
                    List<Integer> list1 = new ArrayList<>();
                    list1.add(nums[left]);
                    list1.add(nums[right]);
                    list1.add(nums[i]);
                    list2.add(list1);
                    left++;
                    right--;
                    while(left < right && nums[left] == nums[left - 1]) {
                        left++;
                    }
                    while(left < right && nums[right] == nums[right - 1]) {
                        right--;
                    }
                }
            }
            i++;
            while(i < nums.length && nums[i] == nums[i - 1]) {
                i++;
            }
        }
        return list2;
    }

    public static void main(String[] args) {
        int[] nums = {-1,0,1,2,-1,-4,-2,-3,3,0,4};
        List<List<Integer>> list = new ArrayList<>();
        list = threeSum(nums);
        System.out.println(list);
    }
}